package com.wave.wavesystem.serve.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import com.mybatisflex.core.query.QueryChain;
import com.wave.common.enums.ErrorCode;
import com.wave.common.model.Result;
import com.wave.common.util.Func;
import com.wave.wavesystem.serve.pojo.dto.user.UserLoginDTO;
import com.wave.wavesystem.serve.pojo.dto.user.UserRegisterDTO;
import com.wave.wavesystem.serve.pojo.entity.User;
import com.wave.wavesystem.serve.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Tag(name = "权限接口-AuthController")
public class AuthController {

    private final IUserService userService;

    private final StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public Result<User> login(@RequestBody UserLoginDTO loginDTO) {
        if (Func.isAnyEmpty(loginDTO, loginDTO.getAccount(), loginDTO.getPassword(),
                loginDTO.getCaptcha(), loginDTO.getOrgCaptcha())) {
            return Result.fail(ErrorCode.PARAMS_ERROR, "缺少请求参数");
        }
//        if (!loginDTO.getCaptcha().equalsIgnoreCase(loginDTO.getOrgCaptcha())) {
//            return Result.fail(ErrorCode.PARAMS_ERROR, "验证码错误");
//        }
        QueryChain<User> queryChain = userService.queryChain();
        User one = queryChain.eq(User::getAccount, loginDTO.getAccount()).one();
        if (Func.isEmpty(one)) {
            return Result.fail();
        }
        String mdPassword = getMdPassword(one.getSalt(), loginDTO.getPassword());
        if (!mdPassword.equals(one.getPassword())) {
            return Result.fail();
        }
        StpUtil.login(one.getId());
        userService.updateChain().eq(User::getId, one.getId())
                .setRaw(User::getLevelExp, "levelExp + 1 ")
                .setRaw(User::getTotalExp, "totalExp + 1 ")
                .update();
        User user = userService.getById(one.getId());
        Integer userLevelRank = userService.getUserLevelRank(one.getId());
        user.setRank(userLevelRank);
        return Result.success(user);
    }

    @PostMapping("/register")
    @Operation(summary = "注册接口")
    public Result<User> login(@RequestBody UserRegisterDTO registerDTO) {
        QueryChain<User> queryChain = userService.queryChain();
        User one = queryChain.eq(User::getAccount, registerDTO.getAccount()).one();
        if (Func.isNotEmpty(one)) {
            return Result.fail();
        }
        String salt = RandomUtil.randomString(10);
        User user = new User();
        user.setAccount(registerDTO.getAccount());
        user.setSalt(salt);
        user.setUsername(RandomUtil.randomString(10));
        String mdPassword = getMdPassword(salt, registerDTO.getPassword());
        user.setPassword(mdPassword);
        userService.save(user);
        return Result.success(user);
    }

    private String getMdPassword(String salt, String password) {
        return DigestUtils.md5DigestAsHex((salt + password).getBytes());

    }
}
